import 'package:cash_drawer/common/common.dart';
import 'package:cash_drawer/ui/splash_page.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter_des/flutter_des.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:package_info/package_info.dart';

import 'common/application.dart';
import 'common/router_config.dart';
import 'common/user.dart';
import 'data/dio_manager.dart';
import 'event/login_event.dart';
import 'event/theme_change_event.dart';
import 'utils/theme_util.dart';

final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();

void main() async{
//  if (!await _requestPermissions()) {
//    await AppUtils.popApp();
//  } else {
    runApp(new MyApp());
//  }

}

void example() async {
  const string = "Java, android, ios, get the same result by DES encryption and decryption.";
  const key = "u1BvOHzUOcklgNpn1MaWvdn9DT4LyzSX";
  const iv = "12345678";
  print("  测试加密     : "+iv);
  var encrypt = await FlutterDes.encrypt(string, key, iv: iv);
  var decrypt = await FlutterDes.decrypt(encrypt, key, iv: iv);
  var encryptHex = await FlutterDes.encryptToHex(string, key, iv: iv);
  var decryptHex = await FlutterDes.decryptFromHex(encryptHex, key, iv: iv);
  var encryptBase64 = await FlutterDes.encryptToBase64(string, key, iv: iv);
  var decryptBase64 = await FlutterDes.decryptFromBase64(encryptBase64, key, iv: iv);
  print("   decryptBase64   : "+decryptBase64);
}

class MyApp extends StatefulWidget {


  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState

    example();
    return new _AppState();
  }
}

Future<bool> _requestPermissions() async {
  Map<PermissionGroup, PermissionStatus> permissions =
  await PermissionHandler().requestPermissions([
    PermissionGroup.storage,
    PermissionGroup.location,
  ]);


  List<bool> results = permissions.values.toList().map((status) {
    return status == PermissionStatus.granted;
  }).toList();


  return !results.contains(false);
}

class _AppState extends State<MyApp>{
  ThemeData themeData;

  @override
  void initState() {
    // TODO: implement initState

    _initAsync();
    Application.eventBus = new EventBus();
    themeData = ThemeUtils.getThemeData();
    this.registerThemeEvent();
    Future.sync(() => print('同步运行的Future'));
    super.initState();
  }

  void _initAsync() async{
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    AppConfig.deviceInfo = androidInfo;
    AppConfig.packageInfo = await PackageInfo.fromPlatform();
    await DioManager.init();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: AppConfig.appName,
      debugShowCheckedModeBanner: false,
      routes: Router.generateRoute(),
      navigatorKey: navigatorKey,
      home: new SplashPage(),
    );
  }

  /// 注册主题改变事件
  void registerThemeEvent() {
    Application.eventBus
        .on<ThemeChangeEvent>()
        .listen((ThemeChangeEvent onData) => this.changeTheme(onData));
  }

  void changeTheme(ThemeChangeEvent onData) async {
    setState(() {
      themeData = ThemeUtils.getThemeData();
    });
  }
}

class AppUtils {
  static Future<void> popApp() async {
    await SystemChannels.platform.invokeMethod('SystemNavigator.pop');
  }

  static Future checkUpgrade() async {
    try {
      final bool result =
      await ApplicationMethodChannel.main.invokeMethod('checkUpgrade');
      print('result=$result');
    } on PlatformException {
      print('faied');
    }
  }
}
class ApplicationMethodChannel {
  static MethodChannel main = MethodChannel('main');
}